﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NativeUtils
{
    private static NativeUtils _instance;
    public static NativeUtils Instance {
        get {
            if (_instance == null)
            {
                _instance = new NativeUtils();
            }
            return _instance;
        }
    }
    AndroidJavaClass jc = null;
    private NativeUtils(){
        try
        {
            //因为我这个testlibrary不是主的，没跟随程序运行起来，所以需要借助正在运行的activity
            //如果要自己的Activity为主的，那么com.heimai666.testlibrary要跟unity build中playerSetting中的包名要一样，如果我们自己的工程的主的Activity也叫MainActivity就可以不用把自己的AndroidManifest.xml文件放入Plugins/Android下，不是则要，这样编译出来会以这个为主
            //jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            //AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
            jc = new AndroidJavaClass("com.heimai666.testlibrary.MyMainActivity");
            //jc.CallStatic("SetInstance", jo);
        }
        catch (AndroidJavaException e)
        {
            //showText.text = e.ToString();
            Debug.Log(e.ToString());
        }

    }
    /// <summary>
    /// 调用Android  Toast方法
    /// </summary>
    /// <param name="str"></param>
    public string Toast(string str = ""){
        string res = "默认";
        try
        {
            res = jc.CallStatic<string>("Tip", str);
            
        }
        catch (AndroidJavaException e)
        {
            Debug.Log(e.ToString());
            return e.ToString();
        }
        return res;
    }
    /// <summary>
    /// 调用Android  震动方法
    /// </summary>
    public string Shock()
    {
        string res = "默认";
        try
        {
            res = jc.CallStatic<string>("Shock");
        }
        catch (AndroidJavaException e)
        {
            Debug.Log(e.ToString());
            return e.ToString();
        }
        return res;
    }
    /// <summary>
    /// 调用Android  震动方法
    /// </summary>
    public string Other()
    {
        string res = "默认";
        try
        {
            res = jc.Call<string>("Other");
        }
        catch (AndroidJavaException e)
        {
            Debug.Log(e.ToString());
            return e.ToString();
        }
        return res;
    }
}
